package com.iqik.ioc.service.impl;

import com.iqik.ioc.anno.Autowired;
import com.iqik.ioc.anno.Service;
import com.iqik.ioc.anno.Transactional;
import com.iqik.ioc.dao.AccountDao;
import com.iqik.ioc.pojo.Account;
import com.iqik.ioc.service.AccountService;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void transferError(int from, int to) throws Exception {
        System.out.println("业务逻辑--------------ERROR");
        Account fromAccount = accountDao.getAccountByCode(from);
        Account toAccount = accountDao.getAccountByCode(to);

        fromAccount.setMoney(fromAccount.getMoney() - 100f);
        toAccount.setMoney(toAccount.getMoney() + 100f);

        accountDao.updateAccountByCode(fromAccount);
        int a = 1 / 0;
        accountDao.updateAccountByCode(toAccount);
    }

    @Override
    public void transferSuccess(int from, int to) throws Exception {
        System.out.println("业务逻辑--------------SUCCESS");
        Account fromAccount = accountDao.getAccountByCode(from);
        Account toAccount = accountDao.getAccountByCode(to);

        fromAccount.setMoney(fromAccount.getMoney() - 100f);
        toAccount.setMoney(toAccount.getMoney() + 100f);

        accountDao.updateAccountByCode(fromAccount);
        accountDao.updateAccountByCode(toAccount);
    }
}
